package com.project.controller;


import com.common.core.controller.BaseController;
import com.common.core.domain.AjaxResult;
import com.common.core.page.TableDataInfo;
import com.common.utils.poi.ExcelUtil;
import com.github.pagehelper.PageHelper;
import com.project.comom.NumberDealUtils;
import com.project.domain.dto.projectApplyDto.ExportApplyBookMybatisDto;
import com.project.domain.dto.projectApplyDto.ExportHistoryApplyBookMybatisDto;
import com.project.domain.dto.projectReviewDto.*;
import com.project.domain.entity.BizReviewScheme;
import com.project.domain.vo.BizReviewSchemeVo4EditPage;
import com.project.service.IProjectReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/project/review")
public class ProjectReviewController  extends BaseController {

    @Autowired
    IProjectReviewService projectReviewService;

    @GetMapping(value = "/scheme-list")
    public TableDataInfo schemeList()
    {
        startPage();
        List<BizReviewScheme> list =projectReviewService.schemeList();

        return getDataTable(list);
    }

    @GetMapping(value = "/scheme")
    public AjaxResult scheme(@ModelAttribute BizReviewScheme bizReviewScheme)
    {
        return success(projectReviewService.getScheme(bizReviewScheme));
    }

    @PostMapping(value = "/scheme")
    public AjaxResult saveScheme(@RequestBody BizReviewSchemeVo4EditPage bizReviewSchemeVo4EditPage)
    {
        projectReviewService.saveScheme(bizReviewSchemeVo4EditPage);
        return success();
    }

    @PostMapping(value = "/scheme-del")
    public AjaxResult delScheme(@RequestBody HashMap<String,List<String>> args)
    {
        List<String> ids = args.get("ids");
        if (ids.size()>0){
            projectReviewService.delScheme(ids);
        }
        return success();
    }

    /**
     * 评审计划列表页
     * @param dto 参数
     * @return res
     */
    @GetMapping(value = "/plan-list")
    public TableDataInfo planList(@ModelAttribute PlanListDto dto)
    {
        startPage();
        List<HashMap<String, Object>> hashMaps = projectReviewService.planList(dto);
        return getDataTable(hashMaps);
    }

    /**
     * 评审计划列表页 左上角统计
     * @param dto 参数
     * @return res
     */
    @GetMapping(value = "/plan-list-Statistics")
    public AjaxResult planListStatistics(@ModelAttribute PlanListDto dto)
    {
        List<HashMap<String, Integer>> list =projectReviewService.planListStatistics(dto);
        return success(list);
    }

    /**
     * 启动或停止，评审计划
     * @param dto 请求参数
     * @return 执行结果
     */
    @PostMapping(value = "/plan-state")
    public AjaxResult planState(@RequestBody PlanStateDto dto)
    {
        projectReviewService.planState(dto);

        return success();
    }

    /**
     * 管理员批量删除评审计划
     * @param dto 参数
     * @return 结果
     */
    @PostMapping(value = "/plan-del")
    public AjaxResult planDel(@RequestBody DelPlanDto dto)
    {
        ArrayList<String> idList = Optional.ofNullable(dto.getIds())
                .orElse(new ArrayList<>());
        if (idList.isEmpty()){
            return warn("参数异常");
        }
        projectReviewService.planDel(idList);

        return success();
    }

    /**
     * 点击页面“评审项目数（项）”进入已经通过学校审核的 某一applyInfo的book
     * @param dto infoId
     * @return
     */
    @PostMapping(value = "/passedBooks")
    public TableDataInfo passedBooks(@RequestBody PassedBooksReqDto dto)
    {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize(), dto.getOrderBy());
        List<HashMap<String, Object>> list =projectReviewService.passedBooks(dto);
        return getDataTable(list);
    }

    /**
     * 点击“指派专家(调整专家)”获取页面信息
     * @param bookIdList 参数
     * @return res
     */
    @GetMapping(value = "/assignExperts")
    public AjaxResult assignExperts(@RequestParam ArrayList<String> bookIdList)
    {
        ArrayList<AssignExpertSingleLineDto> list =projectReviewService.assignExperts(bookIdList);
        return success(list);
    }

    /**
     * 保存指定的专家
     * @param dtoList 兼容批量配置，因此是一个列表
     * @return 执行结果
     */
    @PostMapping(value = "/assignExperts")
    public AjaxResult saveExperts(@RequestBody List<SaveExpertsReqDto> dtoList)
    {

        int rows =projectReviewService.saveExperts(dtoList);
        return success();
    }


    /**
     * 点击 评审专家数（人） 进行一次检索
     * 获取某个info下所有book的专家总和
     * @param dto 参数
     * @return x
     */
    @PostMapping(value = "/infoExperts")
    public TableDataInfo infoExperts(@RequestBody InfoExpertsReqDto dto)
    {
        // 自调用分页方法
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize(), dto.getOrderBy());
        List<HashMap<String, Object>> list =projectReviewService.infoExperts(dto);
        return getDataTable(list);
    }


    /**
     * 查看某个专家的所有评审项目
     * @param dto 专家id
     * @return x
     */
    @GetMapping(value = "/review-pros")
    public TableDataInfo reviewPros(@ModelAttribute ReviewProsDto dto)
    {
        startPage();
        List<HashMap<String, Object>> list =projectReviewService.reviewPros(dto);
        return getDataTable(list);
    }

    /**
     * m
     * @param dto
     * @return
     */
    @PostMapping(value = "/makeMessage")
    public AjaxResult makeMessage(@RequestBody MakeMsgReqDto dto)
    {
        int res =projectReviewService.makeMessage(dto);
        return success();
    }

    /**
     * 评审人员查看评审（）
     * @param dto
     * @return
     */
    @GetMapping(value = "/my-review")
    public TableDataInfo myReviews(@ModelAttribute MyReviewReqDto dto)
    {
        startPage();
        List<HashMap<String, Object>> list =projectReviewService.myReviews(dto);
        return getDataTable(list);
    }

    /**
     * 点击 评审 按钮进入查看页面
     * @param dto
     * @return
     */
    @GetMapping(value = "/review-details")
    public AjaxResult reviewDetails(@ModelAttribute ViewExpertReviewDetailsReqDto dto)
    {
        ViewExpertReviewResDto res =projectReviewService.reviewDetails(dto);
        return success(res);
    }


    /**
     * 评审人员进行评审
     * @param dto 参数
     * @return x
     */
    @PostMapping(value = "/review-details")
    public AjaxResult SaveReviewDetails(@RequestBody SaveExpertReviewReqDto dto)
    {
        Boolean res =projectReviewService.SaveReviewDetails(dto);
        return success();
    }


    @GetMapping(value = "/opinion-summary")
    public AjaxResult opinionSummary(@RequestParam String bookId)
    {
        ExpertOptionsResDto res =projectReviewService.opinionSummary(bookId);
        return success(res);
    }


    @PostMapping(value = "/opinion-summary")
    public AjaxResult updateOpinionSummary(@RequestBody SaveAdminReviewOptionReqDto dto)
    {
        int  res =projectReviewService.updateOpinionSummary(dto);
        return success();
    }

    /**
     * 评审项目数（学校通过）
     * @param response x
     * @param dto x
     */
    @PostMapping(value = "/excel-schoolPass")
    public void excel(HttpServletResponse response, @RequestBody PassedBooksExcelReqDto dto)
    {
        dto.setSource(PassedBooksExcelReqDto.SCHOOL_PASS);
        ArrayList<ExportApplyBookMybatisDto> dataList = projectReviewService.exportExcel(dto);
        //region 元-->万元
        for (ExportApplyBookMybatisDto o : dataList) {
            o.setApplyFee(NumberDealUtils.divide_10000(o.getApplyFee()));
        }
        //endregion
        ExcelUtil<ExportApplyBookMybatisDto> util = new ExcelUtil<>(ExportApplyBookMybatisDto.class);
        util.showColumn(dto.getCols().toArray(new String[0]));
        util.exportExcel(response, dataList, "基本科研业务费项目申报材料导出");
    }

    /**
     * 和/passedBooks的唯一区别是，/passedBooks的条件是学校通过，这里的条件是已立项
     * @param dto x
     * @return x
     */
    @PostMapping(value = "/lixiang-list")
    public TableDataInfo lixiangList(@RequestBody LixiangListReqDto dto)
    {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize(), dto.getOrderBy());
        List<HashMap<String, Object>> list =projectReviewService.lixiangList(dto);
        return getDataTable(list);
    }

    /**
     * 导出 批准立项数（学校通过）
     * @param response x
     * @param dto x
     */
    @PostMapping(value = "/excel-lixiang")
    public void excelLixiang(HttpServletResponse response, @RequestBody PassedBooksExcelReqDto dto)
    {
        dto.setSource(PassedBooksExcelReqDto.LI_XIANG);
        ArrayList<ExportApplyBookMybatisDto> dataList = projectReviewService.exportExcel(dto);
        ExcelUtil<ExportApplyBookMybatisDto> util = new ExcelUtil<>(ExportApplyBookMybatisDto.class);
        util.showColumn(dto.getCols().toArray(new String[0]));
        util.exportExcel(response, dataList, "基本科研业务费项目申报材料导出");
    }



    /**
     * 评审专家可以 一次多选已经暂存的评审数据进行提交
     * 实际是，将多个expert_review的状态由 notSubmit改为submit
     * @param dto
     * @return
     */
    @PostMapping(value = "/submit")
    public AjaxResult submit(@RequestBody ExpertSubmitDto dto)
    {
        if (dto.getIds() == null || dto.getIds().isEmpty()){
            return warn("请选择评审数据");
        }
        int affectRows = projectReviewService.submit(dto);
        return success();
    }

    /**
     * 评审专家拒绝评审
     * @param dto 参数
     * @return x
     */
    @PostMapping(value = "/refuse")
    public AjaxResult refuse(@RequestBody RefuseReviewDto dto)
    {
        int rowsCount = projectReviewService.refuseReview(dto);
        return success();
    }

}
